package com.example.my_objectives.controller;

import java.util.ArrayList;
import java.util.List;

import com.example.my_objectives.model.Objective;
import com.example.my_objectives.model.TableName;
import com.example.my_objectives.SpinnerObject;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class NewObjectiveController extends DatabaseController {

	Objective	_model;

	public NewObjectiveController(Context context) {
		super(context);
		_model = new Objective(get_database());
	}

	public Exception add(String name, String description, double value, long id_unit, long id_recurrence,
			boolean is_notifiable, String date, String time) {
		Exception eReturnValue = null;
		try {
			_model = new Objective(get_database(), -1, name, description, value, id_unit, id_recurrence, is_notifiable,
					date, time);
			eReturnValue = _model.add();
		}// try
		catch (Exception e) {
			eReturnValue = e;
		}// catch
		return eReturnValue;
	}

	/**
	 *	All units
	 * @return All records in the table
	 */
	public List<SpinnerObject> getAllUnits() {

		List<SpinnerObject> list = new ArrayList<SpinnerObject>();
		try {
			String query = "SELECT * FROM "+ TableName.unit.toString() +" ORDER BY name";
			Log.d(NewObjectiveController.class.getName(), query);

			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
//				list.add(new SpinnerObject(-1, "Please choose"));
				while (!cursor.isAfterLast()) {
					list.add(new SpinnerObject(cursor.getLong(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("name"))));
					cursor.moveToNext();
				}//while
			}//if
			cursor.close();
		}//try
		catch (Exception e) {
			Log.e(NewObjectiveController.class.toString(), e.getMessage());
			list = null;
		}//catch

		return list;
	}


	/**
	 *	All recurrence
	 * @return All records in the table
	 */
	public List<SpinnerObject> getAllRecurrences() {

		List<SpinnerObject> list = new ArrayList<SpinnerObject>();
		try {
			String query = "SELECT * FROM "+ TableName.recurrence.toString() + " ORDER BY id DESC";
			Log.d(NewObjectiveController.class.getName(), query);

			Cursor cursor = get_database().rawQuery(query, null);

			if (cursor.moveToFirst()) {
//				list.add(new SpinnerObject(-1, "Please choose"));
				while (!cursor.isAfterLast()) {
					list.add(new SpinnerObject(cursor.getLong(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("name"))));
					cursor.moveToNext();
				}//while
			}//if
			cursor.close();
		}//try
		catch (Exception e) {
			Log.e(NewObjectiveController.class.toString(), e.getMessage());
			list = null;
		}//catch

		return list;
	}


}
